package com.tools.api;

import com.tools.model.params.UserParams;
import com.tools.security.base.AuthToken;
import com.tools.service.AuthService;
import com.tools.commons.annotation.AnonymousAccess;
import com.tools.commons.annotation.Log;
import com.tools.commons.constants.SystemConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：登陆注销")
@RequestMapping(SystemConstants.Api.Auth)
public class AuthorizationController {
    private final AuthService authService;

    @Log(description = "登录")
    @ApiOperation("短信验证码登录")
    @PostMapping(value = "loginInCode")
    @AnonymousAccess()
    public AuthToken login(@RequestBody
                           @Validated(UserParams.loginInCode.class) UserParams loginParams) {
        return authService.login(loginParams);
    }

    @Log(description = "获取短信验证码")
    @ApiOperation("获取短信验证码")
    @PostMapping(value = "sendCode/{phone}")
    @AnonymousAccess()
    public String code(@PathVariable String phone) {
        authService.sendCode(phone);
        return null;
    }

    @PostMapping("logout")
    @ApiOperation("注销")
    @AnonymousAccess
    public String logout() {
        authService.logout();
        return null;
    }
}
